﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Livraria
{
    public partial class AlterarProduto : System.Web.UI.Page
    {
        SqlConnection conn;
        //protected string strConnectionString = @"Data Source=srv-acd-sql;Initial Catalog=bdAsp_lucas;Integrated Security=True";
        protected string strConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + "C:\\Arquivos de programas\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\bdAsp_Livraria.mdf" + ";Integrated Security=True;Connect Timeout=30;User Instance=True";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_pesq_Click(object sender, EventArgs e)
        {
            pnl_id.Enabled = true;
            if (lbl_erro.Visible)
            {
                lbl_erro.Visible = false;
            }
            Limpar();
            try
            {
                conn = new SqlConnection(strConnectionString);

                string textid = txt_id.Text;

                string sqlSelect = "select * from Produtos where id = " + textid + "";

                SqlCommand cmd = new SqlCommand(sqlSelect, conn);
                cmd.Connection.Open();
                SqlDataReader reader;
                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    pnl_result.Enabled = true;
                    lbl_idfix.Text = "ID: " + txt_id.Text;
                    txt_nome.Text = (string)reader["nome"];
                    txt_categ.Text = (string)reader["categoria"];
                }
                else
                {
                    throw new Exception();
                }

                cmd.Connection.Close();
            }
            catch (SqlException)
            {
                lbl_erro.Text = "Erro ao tentar acessar o banco.";
            }
            catch (Exception ex)
            {
                lbl_erro.Visible = true;
                lbl_erro.Text = "Erro ao consultar o produto especificado.";

                pnl_id.Enabled = true;
            }
        }

        protected void btn_atualizar_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(strConnectionString);

            string textid = txt_id.Text;
            string nome = txt_nome.Text;
            string categ = txt_categ.Text;

            string sqlUpdate = "update Produtos set nome = '" + nome + "', categoria  = '" + categ + "' where id = " + textid + "";
            
            SqlCommand cmd = new SqlCommand(sqlUpdate, conn);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            Limpar();
            pnl_id.Enabled = false;
        }

        protected void Limpar()
        {
            txt_nome.Text = "";
            txt_categ.Text = "";
        }        
    }
}